package com.locus.locus.client.widgets;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;

public final class WidgetMenu extends Composite {
	
	public static final class Link{
		
		private final String url;
		private final String texto;
		private final String token;

		public Link(String url, String texto, String token) {
			this.url = url;
			this.texto = texto;
			this.token = token;
		}
	}

	private static WidgetMenuUiBinder uiBinder = GWT
			.create(WidgetMenuUiBinder.class);

	interface WidgetMenuUiBinder extends UiBinder<Widget, WidgetMenu> {
	}

	@UiField
	HorizontalPanel painel;
	
	private final class CallbackSelecao implements ClickHandler {	
		private final String token;

		public CallbackSelecao(String token) {
			this.token = token;
		}

		@Override
		public void onClick(ClickEvent event) {
			History.newItem(this.token);
		}
	};

	public WidgetMenu(List<Link> links, ValueChangeHandler<String> callbackSelecaoMenu) {
		initWidget(uiBinder.createAndBindUi(this));
		Anchor itemMenu;
		for(Link l:links){
			itemMenu = new Anchor(l.texto, l.url);
			itemMenu.addClickHandler(new CallbackSelecao(l.token));
			this.painel.add(itemMenu);
		}
		
		History.addValueChangeHandler(callbackSelecaoMenu);
	}
}
